package com.wwwarehouse.carddesk.fragment.functionlogincontrol;

import android.os.Bundle;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.carddesk.R;
import com.wwwarehouse.carddesk.bean.CardDeskGroupresponseBean;
import com.wwwarehouse.carddesk.constant.CardDeskConstant;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.fragment.CommonBasePagerFragment;
import com.wwwarehouse.common.http.NoHttpUtils;
import com.wwwarehouse.common.tools.LogUtils;

import java.util.HashMap;
import java.util.Map;


/**
 * 团队框架 ---- 暂停开启登录
 * minifly
 * 2017:11:15 9:30
 *
 * @author minfily
 */

public class FunctionLoginControlSearchresultPagerFragment extends CommonBasePagerFragment implements View.OnClickListener {

    private int pageSize = 7;
    private int total;
    private CardDeskGroupresponseBean bean;
    private String buId;
    private String name;
    private static final int REQUEST_ALLPEOPLE = 10004;

    @Override
    public void setTitle() {
        if (peekFragment() instanceof FunctionLoginControlSearchresultPagerFragment) {
            mActivity.setTitle("" + getString(R.string.teamdesk_login_control));
        }
    }

    @Override
    public void onDownPull() {
    }

    @Override
    public void requestDatas() {
        super.requestDatas();

        total = getArguments().getInt("total");
        bean = getArguments().getParcelable("bean");
        buId = getArguments().getString("buId");
        name = getArguments().getString("name");

        //没有给实体
        if(bean == null ){
            getAllPeople();
        }//给了实体
        else{
            initPager();
        }


    }

    private void initPager() {
        Bundle bundle = new Bundle();

        bundle.putString("buId",buId);
        bundle.putString("name",name);

        try {
            setData(total,pageSize ,new FunctionLoginControlSearchresultFragment().getClass().getName(), bundle, bean.getList());
        } catch (Exception e) {
            e.printStackTrace();
            LogUtils.showErrLog("error:"+e.getMessage());
        }
    }

    public void getAllPeople() {
        Map<String, Object> map = new HashMap<>();
        /**
         * buId	业务单位	number	@mock=260001
         name	名字	string	@mock=
         page	页	number
         size	一页大小	number
         */
        map.put("buId", buId);
        map.put("name", "" + name);
        map.put("page", 1);
        map.put("size", pageSize);
        NoHttpUtils.httpPost(CardDeskConstant.PATH_CARDDESK_GROUP_STOPLOGINGETALLPEPLE, map, myOnresponse, REQUEST_ALLPEOPLE);
        showProgressDialog();
    }


    NoHttpUtils.OnResponseListener myOnresponse = new NoHttpUtils.OnResponseListener() {
        @Override
        public void onSucceed(CommonClass commonClass, int what) {
            switch (what) {
                case REQUEST_ALLPEOPLE:
                    if("0".equals(commonClass.getCode())){
                        bean = JSON.parseObject(commonClass.getData().toString(), CardDeskGroupresponseBean.class);
                        total = bean.getTotal();
                        initPager();
                    }else{
                        toast("" + commonClass.getMsg());
                    }
                    break;
                default:

                    break;
            }

        }

        @Override
        public void onFailed(String result, int what) {

        }

        @Override
        public void onFinish(int what) {
            dismissProgressDialog();
        }
    };


    @Override
    public void onRefreshListener() {
    }

    @Override
    public void onLoadSuccess(String data, int what) {

    }


    @Override
    public void onLoadError(String s) {

    }

    @Override
    public void onClick(View v) {

    }

}
